unit TeacherDataForm;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, ExtCtrls, Buttons,
  TntForms, TntButtons, TntStdCtrls, ImgList, TntExtCtrls,
  TntClasses;

type

  TLoginState = (lsNo, lsNoChecked, lsOK, lsFail);

  TTeacherForm = class(TTntForm)
    PanelMain: TTntPanel;
    LabeledEditLogin: TTntEdit;
    TntLabel1: TTntLabel;
    TntLabel2: TTntLabel;
    LabeledEditPassword: TTntEdit;
    LabeledEditName: TTntEdit;
    TntLabel3: TTntLabel;
    TntLabel4: TTntLabel;
    LabeledEditSurname: TTntEdit;
    ImageList: TImageList;
    ButtonCheckLogin: TTntImage;
    TntLabel6: TTntLabel;
    LabeledEditCathedra: TTntEdit;
    TntLabel7: TTntLabel;
    ComboBoxPosition: TTntComboBox;
    PanelBottom: TTntPanel;
    BitBtnOk: TTntBitBtn;
    BitBtnCancel: TTntBitBtn;
    LabelError: TTntLabel;
    procedure TntFormShow(Sender: TObject);
    procedure LabeledEditLoginChange(Sender: TObject);
    procedure LabeledEditSurnameChange(Sender: TObject);
    procedure LabeledEditLoginKeyPress(Sender: TObject; var Key: Char);
    procedure ButtonCheckLoginClick(Sender: TObject);
    procedure BitBtnOkClick(Sender: TObject);
    procedure LabeledEditPasswordChange(Sender: TObject);
  private
    fLoginsChangManualy: Boolean;
  private
    procedure SetLoginState(aState: TLoginState);
    procedure CheckValues;
    procedure SetPicture(aButton: TTntImage; aPictureNumber: Integer);
    function CheckLogin(aLogin: WideString): Boolean;
    function IsDataChanged: Boolean;
  public
    Host, Login, Password: string;
    TeacherSurname, TeacherName, TeacherLogin, TeacherPassword, TeacherCathedra,
    TeacherPosition: WideString;
    NewTeacherSurname, NewTeacherName, NewTeacherLogin, NewTeacherPassword, NewTeacherCathedra,
    NewTeacherPosition: WideString;
  end;

implementation

uses
  uInterfaces,
  uRequestImpl,
  uSplashImpl,
  uUtils,
  uTexts;

{$R *.dfm}

{ TStudentForm }

function TTeacherForm.CheckLogin(aLogin: WideString): Boolean;
var
  request: IgtRequest;
  response: IgtResponse;
  splash: IgtSplash;
begin
  splash := NewSplash(Self);
  splash.ShowSplash(cCheckLoginUnical);
  try
    request := NewRequest(Host, Login, Password);
    request.Param['Teacher-login'] := aLogin;
    response := request.ExecSync('GETTEACH');
    Result := response.Code = 301;
  finally
    splash.HideSplash;
  end;
end;

procedure TTeacherForm.CheckValues;
var
  ok: boolean;
  message: WideString;
begin
  ok := True;
  if LabeledEditLogin.Text = '' then
    begin
    message := cEmptyLogin;
    ok := False;
    end;
  if ok and (LabeledEditPassword.Text = '') then
    begin
    message := cEmptyPassword;
    ok := False;
    end;
  if ok and not IsShortText(LabeledEditLogin.Text) then
    begin
    message := cLongLogin;
    ok := False;
    end;
  if ok and not IsShortText(LabeledEditPassword.Text) then
    begin
    message := cLongPassword;
    ok := False;
    end;
  if ok and not IsShortText(LabeledEditName.Text) then
    begin
    message := cLongName;
    ok := False;
    end;
   if ok and not IsShortText(LabeledEditSurname.Text) then
    begin
    message := cLongSurname;
    ok := False;
    end;
  if ok and not IsShortText(LabeledEditCathedra.Text) then
    begin
    message := cLongCathedra;
    ok := False;
    end;

  BitBtnOk.Enabled := ok;
  if not ok then
    LabelError.Caption := message
  else
    LabelError.Caption := '';
end;

function TTeacherForm.IsDataChanged: Boolean;
begin
  if TeacherLogin <> '' then
    Result := (TeacherLogin <> LabeledEditLogin.Text) or
      (TeacherName <> LabeledEditName.Text) or
      (TeacherSurname <> LabeledEditSurname.Text) or
      (TeacherPassword <> LabeledEditPassword.Text) or
      (TeacherCathedra <> LabeledEditCathedra.Text) or
      (TeacherPosition <> ComboBoxPosition.Text)
  else
    Result := ('' <> LabeledEditLogin.Text) or
      ('' <> LabeledEditName.Text) or
      ('' <> LabeledEditSurname.Text) or
      ('' <> LabeledEditPassword.Text) or
      ('' <> LabeledEditCathedra.Text) or
      ('' <> ComboBoxPosition.Text);
end;

procedure TTeacherForm.LabeledEditLoginChange(Sender: TObject);
begin
  if Trim(LabeledEditLogin.Text) <> '' then
    SetLoginState(lsNoChecked)
  else
    SetLoginState(lsNo);
  CheckValues;
end;

procedure TTeacherForm.LabeledEditLoginKeyPress(Sender: TObject; var Key: Char);
begin
  fLoginsChangManualy := Trim(LabeledEditLogin.Text) <> '';
end;

procedure TTeacherForm.LabeledEditPasswordChange(Sender: TObject);
begin
  CheckValues;
end;

procedure TTeacherForm.LabeledEditSurnameChange(Sender: TObject);
begin
  if not fLoginsChangManualy then
    begin
    LabeledEditLogin.Text := TranslitKir2Lat(LabeledEditSurname.Text);
    if LabeledEditName.Text <> '' then
      LabeledEditLogin.Text := LabeledEditLogin.Text + '_' + TranslitKir2Lat(LabeledEditName.Text);
    end;
end;

procedure TTeacherForm.SetLoginState(aState: TLoginState);
begin
  case aState of
    lsNo:
      begin
      SetPicture(ButtonCheckLogin, 0);
      ButtonCheckLogin.Hint := '';
      ButtonCheckLogin.Enabled := False;
      end;
    lsNoChecked:
      begin
      SetPicture(ButtonCheckLogin, 1);
      ButtonCheckLogin.Hint := cCheckLoginAvailable;
      ButtonCheckLogin.Enabled := True;
      end;
    lsOK:
      begin
      SetPicture(ButtonCheckLogin, 2);
      ButtonCheckLogin.Hint := cLoginUnical;
      ButtonCheckLogin.Enabled := False;
      end;
    lsFail:
      begin
      SetPicture(ButtonCheckLogin, 3);
      ButtonCheckLogin.Hint := cTeacherAlreadyExists;
      ButtonCheckLogin.Enabled := False;
      end;
  end;
end;

procedure TTeacherForm.SetPicture(aButton: TTntImage;
  aPictureNumber: Integer);
var
  btm: TBitmap;
begin
btm := TBitmap.Create;
try
  ImageList.GetBitmap(aPictureNumber, btm);
  btm.TransparentColor := clWhite;
  aButton.Picture.Bitmap := btm;
  aButton.Transparent := True;
finally
  btm.Free;
end;
end;

procedure TTeacherForm.TntFormShow(Sender: TObject);
begin
  LabeledEditLogin.Text := TeacherLogin;
  if TeacherLogin <> '' then
    begin
    SetLoginState(lsOK);
    fLoginsChangManualy := True;
    end
  else
    begin
    SetLoginState(lsNo);
    fLoginsChangManualy := False;
    end;
  LabeledEditPassword.Text := TeacherPassword;
  LabeledEditName.Text := TeacherName;
  LabeledEditSurname.Text := TeacherSurname;
  LabeledEditCathedra.Text := TeacherCathedra;
  if TeacherPosition = '' then
    ComboBoxPosition.ItemIndex := 0
  else
    ComboBoxPosition.ItemIndex := ComboBoxPosition.Items.IndexOf(TeacherPosition);
  CheckValues;
end;

procedure TTeacherForm.BitBtnOkClick(Sender: TObject);
var
  request: IgtRequest;
  response: IgtResponse;
  splash: IgtSplash;
begin
  if not IsDataChanged then
    begin
    ModalResult := mrCancel;
    Exit;
    end;

  splash := NewSplash(Self);
  try
    request := NewRequest(Host, Login, Password);
    if TeacherLogin = '' then
      begin
      splash.ShowSplash(cAddTeacher);
      request.Param['Teacher-login'] := LabeledEditLogin.Text;
      request.Param['Teacher-password'] := LabeledEditPassword.Text;
      request.Param['Teacher-name'] := LabeledEditName.Text;
      request.Param['Teacher-surname'] := LabeledEditSurname.Text;
      request.Param['Teacher-pulpit'] := LabeledEditCathedra.Text;
      if ComboBoxPosition.ItemIndex <> 0 then
        request.Param['Teacher-job'] := ComboBoxPosition.Text;
      response := request.ExecSync('ADDTEACH');
      end
    else
      begin
      splash.ShowSplash(cEditTeacher);
      request.Param['Teacher-login'] := TeacherLogin;
      request.Param['Teacher-newlogin'] := LabeledEditLogin.Text;
      request.Param['Teacher-newpassword'] := LabeledEditPassword.Text;
      request.Param['Teacher-newname'] := LabeledEditName.Text;
      request.Param['Teacher-newsurname'] := LabeledEditSurname.Text;
      request.Param['Teacher-newpulpit'] := LabeledEditCathedra.Text;
      if ComboBoxPosition.ItemIndex <> 0 then
        request.Param['Teacher-newjob'] := ComboBoxPosition.Text;
      response := request.ExecSync('EDITTEACH');
      end;
    case response.Code of
      200: ModalResult := mrOk;
      302: begin
           ShowWarningMessage(cTeacherAlreadyExists, Self);
           ModalResult := mrNone;
           Exit;
           end
    else
      begin
      ShowWarningMessage(response.MessageText);
      ModalResult := mrNone;
      Exit;
      end;
    end;
    NewTeacherLogin := LabeledEditLogin.Text;
    NewTeacherPassword := LabeledEditPassword.Text;
    NewTeacherName := LabeledEditName.Text;
    NewTeacherSurname := LabeledEditSurname.Text;
    NewTeacherCathedra := LabeledEditCathedra.Text;
    NewTeacherPosition := ComboBoxPosition.Text;
  finally
    splash.HideSplash;
  end;
  ModalResult := mrOk;
end;

procedure TTeacherForm.ButtonCheckLoginClick(Sender: TObject);
begin
  if CheckLogin(LabeledEditLogin.Text) then
    SetLoginState(lsOK)
  else
    SetLoginState(lsFail);
end;

end.
